package cxt.com.recyclerview.demo3;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import java.util.ArrayList;
import java.util.List;

import cxt.com.recyclerview.R;
import cxt.com.recyclerview.demo1.DataModel;

/**
 * RecycleView item 交换位置
 */


interface OnStartDragListener {
    void startDrag(RecyclerView.ViewHolder holder);
}

public class MainActivity3 extends AppCompatActivity implements OnStartDragListener {
    private RecyclerView mRecyclerView;
    private NormalAdapter mNormalAdapter;
    private ItemTouchHelper mItemTouchHelper;
    private List<DataModel> modelList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        initView();
    }


    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mNormalAdapter = new NormalAdapter(modelList = initData(), this);
        mRecyclerView.setAdapter(mNormalAdapter);
        mItemTouchHelper = new ItemTouchHelper(new SimpleItemTouchCallback(mNormalAdapter, modelList));
        mItemTouchHelper.attachToRecyclerView(mRecyclerView);
    }

    public ArrayList<DataModel> initData() {
        ArrayList<DataModel> models = new ArrayList<>();
        String[] titles = getResources().getStringArray(R.array.title_array);
        for (int i = 0; i < titles.length; i++) {
            DataModel model = new DataModel();
            model.setNumber(i + 1);
            model.setTitle(titles[i]);
            models.add(model);
        }
        return models;
    }


    @Override
    public void startDrag(RecyclerView.ViewHolder holder) {
        mItemTouchHelper.startDrag(holder);
    }
}